package com.pivotal.cloud.crypto.utils;

import com.pivotal.cloud.crypto.codec.Utf8;

import java.security.MessageDigest;

/**
 * @className: com.pivotal.cloud.security.utils.EncodingUtils
 * @projectName: PivotalCloud项目
 * @module: PivotalCloud项目-EncodingUtils类，主要位于Security安全模块-编码工具类模块
 * @content: EncodingUtils-编码工具类
 * @author: Powered by Marklin
 * @datetime: 2025-06-24 15:28
 * @version: 1.0.0
 * @copyright: Copyright © 2018-2025 PivotalCloud Systems Incorporated. All rights
 * reserved.
 */
public class EncodingUtil {

	private EncodingUtil() {
	}

	public static byte[] concatenate(byte[]... arrays) {
		int length = 0;
		byte[][] var2 = arrays;
		int destPos = arrays.length;

		for (int var4 = 0; var4 < destPos; ++var4) {
			byte[] array = var2[var4];
			length += array.length;
		}

		byte[] newArray = new byte[length];
		destPos = 0;
		byte[][] var9 = arrays;
		int var10 = arrays.length;

		for (int var6 = 0; var6 < var10; ++var6) {
			byte[] array = var9[var6];
			System.arraycopy(array, 0, newArray, destPos, array.length);
			destPos += array.length;
		}

		return newArray;
	}

	public static byte[] subArray(byte[] array, int beginIndex, int endIndex) {
		int length = endIndex - beginIndex;
		byte[] subarray = new byte[length];
		System.arraycopy(array, beginIndex, subarray, 0, length);
		return subarray;
	}

	public static boolean equals(String expected, String actual) {
		byte[] expectedBytes = bytesUtf8(expected);
		byte[] actualBytes = bytesUtf8(actual);
		return MessageDigest.isEqual(expectedBytes, actualBytes);
	}

	private static byte[] bytesUtf8(String s) {
		return (s != null) ? Utf8.encode(s) : null;
	}

}
